package com.atguigu.Stack;

import java.sql.PreparedStatement;

/**
 * description:
 * Created by 才睡醒 on 2021-06-09
 */
public class LinkedListStackDemo {
    public static void main(String[] args) {
        LinkedListStack linkedListStack = new LinkedListStack();
        linkedListStack.push(new Node(1));
        linkedListStack.push(new Node(2));
        linkedListStack.push(new Node(3));
        linkedListStack.push(new Node(4));
        linkedListStack.pop();
        linkedListStack.pop();
        linkedListStack.pop();
    }

}

//创建一个链表模拟栈
class LinkedListStack {
    //头节点
    private Node head = new Node(-1 );
    //辅助变量
    private Node temp = null;
    //栈空
    public boolean isEmpty() {
        return head.next == null;
    }
    //入栈---采用头插法
    public  void push(Node node){
        if(head.next == null){ //第一个节点的插入
            head.next = node;
            return;
        }
        temp = head.next;
        head.next = node;
        node.next = temp;

    }
    //出栈
    public void pop(){
        if(isEmpty()){
            System.out.println("栈空");
            return;
        }
        System.out.println(head.next);
        head = head.next;

    }
}


//链表元素
class Node {
    public int num;
    public Node next;

    public Node(int num) {
        this.num = num;
    }


    @Override
    public String toString() {
        return "Node{" +
                "num=" + num +
                ", next=" + next +
                '}';
    }
}
